package _06reflect;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Properties;
import java.util.Scanner;

/**
 * @description:
 * @author: 景天
 * @date: 2022/7/18 15:12
 **/

public class Sender {
    static Properties properties = new Properties();

    static {
        InputStream in = ClassLoader.getSystemResourceAsStream("dict.properties");
        // 使用字符流load配置文件
        try {
            properties.load(new InputStreamReader(in, "GBK"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        String sendPort = properties.getProperty("sendPort");
        String receiveIp = properties.getProperty("receiveIp");
        String receivePort = properties.getProperty("receivePort");
        // 创建发送端Socket对象
        DatagramSocket datagramSocket = new DatagramSocket(Integer.parseInt(sendPort));
        Scanner scanner = new Scanner(System.in);
        // while
        while (true) {
            // 键盘接收数据
            String s = scanner.nextLine();
            // 把要发送的数据封装成数据报包
            DatagramPacket sendPacket = NetworkUtils.getSendPacket(s, receiveIp, Integer.parseInt(receivePort));
            // send数据
            datagramSocket.send(sendPacket);
            // 创建接收的数据报包
            DatagramPacket receivePacket = NetworkUtils.getReceivePacket();
            // receive数据
            datagramSocket.receive(receivePacket);
            // 解析数据
            String receiveMsg = NetworkUtils.parseMsg(receivePacket);
            System.out.println(receiveMsg);

        }
    }
}
